﻿using System;
using System.Web;

using Fuse8.DomainFramework.Common;

using Fuse8.ContentManagementFramework.SystemFacade.Common;

namespace Fuse8.ContentManagementFramework.Web.Etc
{
    public class SessionStorage<T> : IStorage<T>
    {
        private static readonly string _typeName = typeof(T).FullName;

        private HttpContextBase _currentContext = null;

        public virtual HttpContextBase CurrentContext
        {
            get
            {
                return _currentContext == null ?
                    new HttpContextWrapper(HttpContext.Current) :
                    _currentContext;
            }
            set
            {
                _currentContext = value;
            }
        }

        #region IStorage<T> Members

        public virtual string StorageKey
        {
            get 
            {
                return CurrentContext.Session.SessionID;
            }
        }

        public virtual T GetProperty()
        {
            return GetProperty(_typeName);
        }

        public virtual T GetProperty(string key)
        {
            Guard.ArgumentNotNull(key, "key");

            return (T)CurrentContext.Session[key];
        }

        public virtual void SetProperty(T value)
        {
            SetProperty(_typeName, value);
        }

        public virtual void SetProperty(string key, T value)
        {
            Guard.ArgumentNotNull(key, "key");

            CurrentContext.Session[key] = value;
        }

        public virtual void Remove()
        {
            Remove(_typeName);
        }

        public virtual void Remove(string key)
        {
            Guard.ArgumentNotNull(key, "key");

            CurrentContext.Session.Remove(key);
        }

        #endregion
    }
}
